package com.pks.autoInsurance.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pks.autoInsurance.dto.RegistrationDto;
import com.pks.autoInsurance.dto.VehicleDto;
import com.pks.autoInsurance.helper.RegistrationHelper;
import com.pks.autoInsurance.helper.UserHelper;

/**
 * Registration process of the user Servlet implementation class
 * 
 * @author Pankaj Soni
 */
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doPost::::begin");
		String task = request.getParameter("task");
		if ("profile-register".equals(task)) {
			profileRegister(request, response);
		} else if ("vehicle-register".equals(task))
			vehicleRegister(request, response);
		System.out.println(getClass() + "::::doPost::::end");
	}

	/*
	 * Capture the user profile details for registration
	 */
	private void profileRegister(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			System.out.println(getClass() + "::::profileRegister::::begin");
			String ssn = request.getParameter("ssn");
			String passwd = request.getParameter("passwd");
			String firstName = request.getParameter("fName");
			String lastName = request.getParameter("lName");
			String gender = request.getParameter("gender");
			String mobileNo = request.getParameter("mobileNo");
			String emailId = request.getParameter("email");
			String city = request.getParameter("city");
			String bloodGroup = request.getParameter("bloodGroup");
			String drivingLicence = request.getParameter("drivingLicence");
			String dateOfBirthString = request.getParameter("dateOfBirth");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd");
			java.util.Date dateOfBirth = null;
			try {
				dateOfBirth = dateFormat.parse(dateOfBirthString);
			} catch (ParseException e) {
				System.out.println("::::" + e.getMessage());
			}

			RegistrationDto registrationDto = new RegistrationDto();
			registrationDto.setSsn(ssn);

			RegistrationHelper registrationHelper = new RegistrationHelper();

			boolean userExistsFlag = registrationHelper
					.isExistingUser(registrationDto);
			if (userExistsFlag) {
				request.setAttribute("error",
						"SSN already registered, you can login directly");
				request.getRequestDispatcher("register.jsp").forward(request,
						response);
				return;
			} else {
				registrationDto.setPasswd(passwd);
				registrationDto.setFirstName(firstName);
				registrationDto.setLastName(lastName);
				registrationDto.setGender(gender);
				registrationDto.setDateOfBirth(dateOfBirth);
				registrationDto.setMobileNo(mobileNo);
				registrationDto.setEmail(emailId);
				registrationDto.setCity(city);
				registrationDto.setBloodGroup(bloodGroup);
				registrationDto.setDrivingLicence(drivingLicence);
			}
			System.out.println("::::populating registrationDto:::::"
					+ registrationDto);
			boolean isUserRegistered = registrationHelper
					.registerUser(registrationDto);
			System.out.println("::::isUserRegistered::::" + isUserRegistered);

			// Set ssn in session and open home page
			HttpSession session = request.getSession(false);
			session.setAttribute("ssn", registrationDto.getSsn());
			request.getRequestDispatcher("home.jsp").forward(request, response);
		} catch (ServletException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		} catch (IOException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		}
		System.out.println(getClass() + "::::profileRegister::::end");
	}

	/*
	 * Capture the vehicle details
	 */
	private void vehicleRegister(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println(getClass() + "::::vehicleRegister::::begin");
		String type = request.getParameter("type");
		String model = request.getParameter("model");
		String make = request.getParameter("make");
		String regNo = request.getParameter("regNo");
		String policyType = request.getParameter("policyType");
		String policyAmount = request.getParameter("policyAmount");
		String mfYear = request.getParameter("mfYear");
		int totalAccident = Integer.parseInt(request
				.getParameter("totalAccident"));

		HttpSession session = request.getSession(false);
		VehicleDto vehicleDto = new VehicleDto();
		vehicleDto.setSsn((String) session.getAttribute("ssn"));
		vehicleDto.setType(type);
		vehicleDto.setModel(model);
		vehicleDto.setMake(make);
		vehicleDto.setRegNo(regNo);
		vehicleDto.setPolicyType(policyType);
		vehicleDto.setPolicyAmount(Integer.parseInt(policyAmount));
		vehicleDto.setMfYear(mfYear);
		vehicleDto.setTotalAccident(totalAccident);
		vehicleDto.setQuoteDate(new Date());
		System.out.println("::::vehicleDto::::" + vehicleDto);
		boolean vehicleRegistered = new RegistrationHelper()
				.registerVehicle(vehicleDto);
		System.out.println("::::vehicleRegistered::::" + vehicleRegistered);
		getQuote(request, response);
		System.out.println(getClass() + "::::vehicleRegister::::end");
	}

	private void getQuote(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.setAttribute("quote", new UserHelper()
					.getQuote((String) request.getSession(false).getAttribute(
							"ssn")));
			request.getRequestDispatcher("quote.jsp")
					.forward(request, response);
			return;
		} catch (ServletException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		} catch (IOException ex) {
			System.err.println(getClass() + "::::" + ex.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doGet::::begin");
		String task = request.getParameter("task");
		if ("quote".equals(task)) {
			getQuote(request, response);
		}
		System.out.println(getClass() + "::::doGet::::end");
	}
}
